﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace StringDefs.Tests
{
    using StringDefs.Web;
    using StringDefs.Business;
    
    [TestFixture]
    public class WebTests
    {
        [Test]
        public void IsValidCreditCardTest()
        {
           // Assert.AreEqual(true, "5210987654321450".IsValidCreditCard());
            Assert.AreEqual(false, "5210987654321451".IsValidCreditCard());
        }
        //The main method in StringDefs.Web is commented so I commented this one out too. 
        //To make sure the TestCase Project runs just fine as of now

        //[Test]
        //public void ExtractBaseDomainTest()
        //{
        //    Assert.AreEqual("www.consulttoday.com", "http://www.consulttoday.com/api.aspx".ExtractBaseDomain());
        //    Assert.AreEqual("consulttoday.com", "ftp://consulttoday.com".ExtractBaseDomain());
        //}
        [Test]
        public void ExtractAnchorUrls_ProperlyExtractsAnchorUrlsForSimpleCase()
        {
            const string text = @"test text <a href=""www.google.com"">Google</a> this was a test";

            var results = text.ExtractHyperlinks();
            
            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("www.google.com", results[0]);
        }

        [Test]
        public void ExtractAnchorUrls_ProperlyExtractsOnlyOneUrlWhenitAppearsTwice()
        {
            const string text = @"test text <a href=""www.google.com"">Google</a> this was a test, <a href=""www.google.com"">Google</a> this was a another test";

            var results = text.ExtractHyperlinks();

            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("www.google.com", results[0]);
        }
        
        [Test]
        public void ExtractUrls_ProperlyExtractsHttpAndFtpUrls()
        {
            const string text = "test text http://www.google.com this was a test, ftp://www.google.com this was a another test";

            var results = text.ExtractUrls();

            Assert.AreEqual(2, results.Count);
            Assert.AreEqual("http://www.google.com", results[0]);
            Assert.AreEqual("ftp://www.google.com", results[1]);
        }

        [Test]
        public void ExtractUrls_ProperlyExtractsWeirdHttpAndFtpUrls()
        {
            const string text = "href=\"http://www.google.com\" href=\"http://www.google.com\" href=\"ftp://www.google.com\""; 

            var results = text.ExtractUrls();

            Assert.AreEqual(2, results.Count);
            Assert.AreEqual("http://www.google.com", results[0]);
            Assert.AreEqual("ftp://www.google.com", results[1]);
        }        

        [Test]
        public void ExtractUrls_ProperlyExtractsHttpAndFtpUrlsOnlyOnceWhenThereAreDuplicates()
        {
            const string text = "test text http://www.google.com this was a test, ftp://www.google.com this was a another test test text http://www.google.com this was a test, ftp://www.google.com this was a another test";

            var results = text.ExtractUrls();

            Assert.AreEqual(2, results.Count);
            Assert.AreEqual("http://www.google.com", results[0]);
            Assert.AreEqual("ftp://www.google.com", results[1]);
        }      
        
        [Test]        
        public void ExtractEmails_ProperlyExtractsMultipleEmails()
        {
            const string emails = "Emails: esupport@xyz.com, blob@blobcity.com, person@yahoo.co.uk,  myname@host.dom";

            var results = emails.ExtractEmailAddresses();
            
            Assert.AreEqual(4, results.Count);
            Assert.AreEqual("esupport@xyz.com", results[0]);
            Assert.AreEqual("blob@blobcity.com", results[1]);
            Assert.AreEqual("person@yahoo.co.uk", results[2]);
            Assert.AreEqual("myname@host.dom", results[3]);
        }

        [Test]
        public void ExtractEmails_ProperlyExtractsMultipleEmailsAndOmmitsDuplicates()
        {
            const string emails = "Emails: esupport@xyz.com, blob@blobcity.com, person@yahoo.co.uk,  myname@host.dom";

            var results = emails.ExtractEmailAddresses();

            Assert.AreEqual(4, results.Count);
            Assert.AreEqual("esupport@xyz.com", results[0]);
            Assert.AreEqual("blob@blobcity.com", results[1]);
            Assert.AreEqual("person@yahoo.co.uk", results[2]);
            Assert.AreEqual("myname@host.dom", results[3]);
        }


        [Test]
        public void ExtractIpAddresses_ProperlyExtractsMultipleIPsAndOmmitsDuplicates()
        {
            const string text = "test text 127.0.0.1, 80.234.20.21,this was an ip also 212.32.212.0";

            var results = text.ExtractIpAddresses();

            Assert.AreEqual(3, results.Count);
            Assert.AreEqual("127.0.0.1", results[0]);
            Assert.AreEqual("80.234.20.21", results[1]);
            Assert.AreEqual("212.32.212.0", results[2]);
        }            
    
    }
}
